package com.woniu.serverdriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.internalcommon.constant.CommonStatusEnum;
import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.serverdriveruser.mapper.DriverUserWorkStatusMapper;
import com.woniu.internalcommon.dto.DriverUserWorkStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DriverUserWorkStatusService {
    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    /**
     *  添加司机工作状态
     * @param driverUserWorkStatus
     */
    public void add(DriverUserWorkStatus driverUserWorkStatus){
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);
    }

    /**
     * 根据司机id查询司机工作状态
     * @param driverId
     * @return
     */
    public ResponseResult<DriverUserWorkStatus> selectById(Long driverId) {
        QueryWrapper<DriverUserWorkStatus> statusQueryWrapper = new QueryWrapper<>();
        statusQueryWrapper.eq("driver_id", driverId);
        List<DriverUserWorkStatus> workStatuses = driverUserWorkStatusMapper.selectList(statusQueryWrapper);
        if (workStatuses.size() == 0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXITST.getCode(), CommonStatusEnum.DRIVER_NOT_EXITST.getValue());
        }
        return ResponseResult.success(workStatuses.get(0));
    }

    /**
     * 修改司机工作状态
     * @param driverUserWorkStatus
     * */
    @Transactional
    public void update(DriverUserWorkStatus driverUserWorkStatus) {
        driverUserWorkStatusMapper.updateById(driverUserWorkStatus);
    }
}
